Thank you everbody, thanks to you I have found a way to correct my program. I did as you told me to do: I removed everything the Constructor object knew about its parent (puting it in the parent instead) and the problem went away. I still don't know from where it was coming, but we can clearly see that having a better-programmed program helps to correct some bugs. ^^
Thanks again to everybody. :D
If you want my corrected code, here it is:
main.cpp (nothing changed)
#include "Window.h" #include <QtWidgets/QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Window w; w.show(); return a.exec(); }Window.h
#ifndef DEF_WINDOW #define DEF_WINDOW #include "Constructor.h" #include <QWidget> #include <QVBoxLayout> #include <QPushButton> #include <vector> class Window : public QWidget { Q_OBJECT public: Window(); public slots: void addConstructor(); void destroyConstructor(Constructor*); private: QVBoxLayout* mainLayout = new QVBoxLayout; QVBoxLayout* constructorLayout = new QVBoxLayout; std::vector<Constructor*>* constructorVector = new std::vector<Constructor*>; QPushButton* addConstructorButton = new QPushButton("+", this); }; #endifWindow.cpp
#include "Window.h" Window::Window() { connect(addConstructorButton, SIGNAL(clicked()), this, SLOT(addConstructor())); mainLayout->addLayout(constructorLayout); mainLayout->addWidget(addConstructorButton); setLayout(mainLayout); } void Window::addConstructor() { Constructor* c = new Constructor(this); constructorLayout->addWidget(c); constructorVector->push_back(c); connect(c, SIGNAL(mustBeDestroyed(Constructor*)), this, SLOT(destroyConstructor(Constructor*))); } void Window::destroyConstructor(Constructor* c) { constructorLayout->removeWidget(c); //remove object from vector for (int i = 0; i < constructorVector->size(); i++) if (constructorVector->at(i) == c) { constructorVector->erase(constructorVector->begin() + i); break; } }Constructor,h
#ifndef DEF_CONSTRUCTOR #define DEF_CONSTRUCTOR #include <QWidget> #include <QLineEdit> #include <QHBoxLayout> #include <QPushButton> #include <QLayout> #include <vector> class Constructor : public QWidget { Q_OBJECT public: Constructor(QWidget* parentWidget); public slots: void destroySelf(); signals: void mustBeDestroyed(Constructor*); private: QLineEdit* lineEdit = new QLineEdit(this); QPushButton* destroyConstructorButton = new QPushButton("-", this); }; #endifConstructor.cpp
#include "Constructor.h" Constructor::Constructor(QWidget* parentWidget) : QWidget(parentWidget) { connect(destroyConstructorButton, SIGNAL(clicked()), this, SLOT(destroySelf())); QHBoxLayout* layout = new QHBoxLayout; layout->addWidget(lineEdit); layout->addWidget(destroyConstructorButton); setLayout(layout); } void Constructor::destroySelf() { delete lineEdit; delete destroyConstructorButton; emit mustBeDestroyed(this); this->disconnect(); delete this; }